package com.imay.live.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.imay.base.fragment.BaseDialogFragment;
import com.imay.live.adapter.PopupMenuAdapter;
import com.imay.live.model.MenuItemModel;
import com.imay.live.view.recyclerview.WrapContentLinearLayoutManager;
import com.imay.live.R;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

/**
 * Created by jeaman on 2017/4/13.
 * 弹出菜单
 */

public class PopupMenuFragment extends BaseDialogFragment implements View.OnClickListener {
    private final static String BUNDLE_NAME_MENU = "menu";
    private RecyclerView mRecyclerView;

    private PopupMenuAdapter mAdapter;

    public static PopupMenuFragment newInstance(ArrayList<MenuItemModel> menuList) {
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(BUNDLE_NAME_MENU, menuList);
        PopupMenuFragment fragment = new PopupMenuFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_popup_menu;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mRecyclerView = $(R.id.popup_menu_rv_recycler);
        initListener();

        ArrayList<MenuItemModel> menuIdList = getArguments().getParcelableArrayList(BUNDLE_NAME_MENU);
        mAdapter = new PopupMenuAdapter(menuIdList);
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).colorResId(R.color.divider_0000)
                .sizeResId(R.dimen.divider_size_popup_menu)
                .build());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initListener() {
        $(R.id.popup_menu_tv_cancel).setOnClickListener(this);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                dismiss();
                if (getActivity() instanceof OnMenuClickListener) {
                    MenuItemModel menu = mAdapter.getItem(position);
                    ((OnMenuClickListener) getActivity()).onMenuClick(menu.getAction());
                }
            }
        });
    }

    @Override
    protected int getWindowAnimations() {
        return R.style.dialog_from_bottom;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getHeight() {
        return WindowManager.LayoutParams.WRAP_CONTENT;
    };

    @Override
    protected boolean isMatchParent() {
        return true;
    }

    @Override
    protected boolean isOutsideTouchable() {
        return true;
    }

    public void setNewMenu(ArrayList<MenuItemModel> menuList) {
        mAdapter.setNewData(menuList);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.popup_menu_tv_cancel:
                dismiss();
                break;
        }
    }

    public interface OnMenuClickListener {
        void onMenuClick(int action);
    }
}
